package com.wbs.shop.nuby.view.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wbs.shop.nuby.R;
import com.wbs.shop.nuby.bean.PayModeBean;
import com.wbs.shop.nuby.utils.GlideUtils;
import com.wbs.shop.nuby.view.base.BaseRecyclerAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhoucl on 2018/5/23 0023.
 */
public class PayModeAdapter extends BaseRecyclerAdapter<PayModeAdapter.ViewHolder, PayModeBean> {
    public PayModeAdapter(Context mContext) {
        super(mContext);
    }

    @Override
    public int getItemLoayoutId() {
        return R.layout.item_cashier_pay_mode;
    }

    @Override
    public ViewHolder onCreateViewHolder(View rootView, int viewType) {
        return new ViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, PayModeBean bean, int position) {
        GlideUtils.load(mContext, bean.getIcon(), holder.image);
//        Glide.with(mContext).load(bean.getIcon()).into(holder.image);
        holder.tvPayModeName.setText(bean.getName());
        holder.tvBalance.setText(bean.getSubName());
        holder.ivCheck.setImageResource(bean.isCheck() ? R.mipmap.icon_check : R.mipmap.icon_uncheck);
        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(this::onClick);
    }

    public void onClick(View v) {
        if (isContainPosition(v)) {
            for (int i = 0; i < getItemCount(); i++) {
                if (getItem(i).isCheck()) {
                    getItem(i).setCheck(false);
                    notifyItemChanged(i);
                }
            }
            int position = getPositionByView(v);
            getItem(position).setCheck(true);
            notifyItemChanged(position);
        }
    }

    public PayModeBean getCheck() {
        for (PayModeBean item : datas) {
            if (item.isCheck()) {
                return item;
            }
        }
        return null;
    }

    public void changCheckByPayMode(int payMode) {
        int checkPosition = -1;
        PayModeBean payModeBean;
        for (int i = 0; i < getItemCount(); i++) {
            payModeBean = getItem(i);
            if (payModeBean.getPaymode() == payMode)
                checkPosition = i;
            if (payModeBean.isCheck()) {
                payModeBean.setCheck(false);
                notifyItemChanged(i);
            }
        }
        if (checkPosition != -1) {
            getItem(checkPosition).setCheck(true);
            notifyItemChanged(checkPosition);
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.image)
        ImageView image;
        @BindView(R.id.tv_pay_mode_name)
        TextView tvPayModeName;
        @BindView(R.id.tv_balance)
        TextView tvBalance;
        @BindView(R.id.iv_check)
        ImageView ivCheck;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
